import re
import requests
import json


def fetch_quote(text):
    return re.findall(r'<span class="text" itemprop="text">([^<]+)', text)[0]


def fetch_author(text):
    return re.findall(r'<small[^>]+>([^<]+)', text)[0]


def fetch_tags(text):
    return re.findall(r'<meta[^>]+ content="([^"]+)"', text)[0].split(',')


def fetch(url):
    r = requests.get(url)
    return r.text


def parse(text):
    res = []
    quotes_texts = re.split(r'class="quote"', text)[1:]
    for quote_text in quotes_texts:
        content = fetch_quote(quote_text)
        author = fetch_author(quote_text)
        tags = fetch_tags(quote_text)
        record = {'content': content, 'author': author, 'tags': tags}
        res.append(record)
    return res


def save(data, filename):
    f = open(filename, 'w')
    text = json.dumps(data)
    f.write(text)


if __name__ == '__main__':
    url = 'http://quotes.toscrape.com/'
    text = fetch(url)
    data = parse(text)
    save(data, 'testing.html')
